from flask import jsonify, request

from myApp.model.message_model import Message
from myApp.model.case_model import CaseDocument
from flask import Blueprint

# from myApp.utils.InitModel import run

case_db = Blueprint('case', __name__)


@case_db.route('/case/page', methods=['POST'])
def get_case_page():
    data = request.json
    search_data = data.get('search_data', "")
    page_number = data.get("page_number", 1)  # 提供默认值为 1
    page_size = data.get("page_size", 10)  # 提供默认值为 10

    DisputeFocus = data.get("DisputeFocus", None)
    CoreFacts = data.get("CoreFacts", None)
    Keywords = data.get("Keywords", None)
    flag = data.get("flag", None)

    if flag == 1:
        Keywords += "/".join(search_data)
        Keywords = Keywords.split("/")

        search_data = {
            "DisputeFocus": DisputeFocus,
            "CoreFacts": CoreFacts,
            "Keywords": Keywords
        }
        print(search_data)

    # 这里假设 CaseDocument.search_page 返回的是一个字典列表
    cases, total = CaseDocument.search_page(search_data, page_number, page_size, flag)

    # 使用 jsonify 返回数据，并且可以设置状态码
    return jsonify({
        "data": {
            "data": [case.serialize() for case in cases],
            "page_number": page_number,
            "page_size": page_size,
            "total": total  # 如果可用，应该使用实际的总数
        },

    }), 200


@case_db.route('/case/<case_id>', methods=['GET'])
def get_case_by_id(case_id):
    case = CaseDocument.get_by_id(case_id)
    if case is None:
        return jsonify({
            "message": "Case not found"
        }), 404
    return jsonify(
        case.serialize()
    ), 200
